// import { query, logout } from '../services/app' //原code
import { logout } from '../services/app'  //改code
// import * as menusService from '../services/menus'
import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from 'config'
import { EnumRoleType } from 'enums'
const { prefix } = config

export default {
  namespace: 'app',
  state: {
    user: {
      id:1,
      permissions: {
        visit:['1', '2', '21','211', '7','9','91','8'],
        role: 'guest',
      },
      userName:'guest'
    },
    permissions: {
      visit: ['1', '2', '21','211', '7','9','91','8'],
      role: 'guest',
    },
    menu: [
      {
        id: 1,
        icon: 'laptop',
        name: '任务进度',
        router: '/dashboard',
      }
    ],
    menuPopoverVisible: false,
    siderFold: localStorage.getItem(`${prefix}siderFold`) === 'true',
    darkTheme: localStorage.getItem(`${prefix}darkTheme`) === 'true',
    isNavbar: document.body.clientWidth < 769,
    navOpenKeys: JSON.parse(localStorage.getItem(`${prefix}navOpenKeys`)) || [],
  },
  subscriptions: {

    setup ({ dispatch }) {
      dispatch({ type: 'query' })
      let tid
      window.onresize = () => {
        clearTimeout(tid)
        tid = setTimeout(() => {
          dispatch({ type: 'changeNavbar' })
        }, 300)
      }
    },

  },
  effects: {

    *query ({
      payload,
    }, { call, put }) {
      /*新代码*/
      let success=true
      const user={
        id:1,
        permissions: {
          visit: ['1', '2', '21','211','22','221','23','231','24','241','25','251','26','261','3', '7','8','9'],
          role: 'guest',
        },
        userName:'guest'
      }

      /*原代码*/
      // const { success, user } = yield call(query, payload);
      if (success && user) {

        /*新代码*/
        const now = new Date()
        now.setDate(now.getDate() + 1)
        var token=JSON.stringify({ id: user.id, deadline: now.getTime() })
        document.cookie='token='+token+',expires='+now.toUTCString()

        /*原代码*/
        // const { list } = yield call(menusService.query)

        /*新代码*/
        let list = [
          {
            id: '1',
            icon: 'laptop',
            name: '任务进度',
            route: '/dashboard',
          },
          {
            id: '2',
            bpid: '1',
            name: '测试配置',
            icon: 'setting',
          },
          {
            id: '21',
            bpid: '2',
            mpid: '2',
            name: '产品配置',
            icon: 'bars',
            route: '/configureList/productConfigList',
          },
          {
            id: '211',
            mpid: '-1',
            bpid: '21',
            name: '产品详情页',
            route: '/configureList/productConfigList/:id',
          },{
            id: '22',
            bpid: '2',
            mpid: '2',
            name: '测试机配置',
            icon: 'bars',
            route: '/configureList/testHostConfigList',
          },
          {
            id: '221',
            mpid: '-1',
            bpid: '22',
            name: '测试机详情页',
            route: '/configureList/testHostConfigList/:id',
          },
          {
            id: '23',
            bpid: '2',
            mpid: '2',
            name: '服务机配置',
            icon: 'bars',
            route: '/configureList/sutConfigList',
          },
          {
            id: '231',
            mpid: '-1',
            bpid: '23',
            name: '服务机详情页',
            route: '/configureList/sutConfigList/:id',
          },
          {
            id: '24',
            bpid: '2',
            mpid: '2',
            name: '测试套件配置',
            icon: 'bars',
            route: '/configureList/testSuiteConfigList',
          },
          {
            id: '241',
            mpid: '-1',
            bpid: '24',
            name: '测试套件详情页',
            route: '/configureList/testSuiteConfigList/:id',
          },
          {
            id: '25',
            bpid: '2',
            mpid: '2',
            name: '测试计划配置',
            icon: 'bars',
            route: '/configureList/testPlanConfigList',
          },
          {
            id: '251',
            mpid: '-1',
            bpid: '25',
            name: '测试计划详情页',
            route: '/configureList/testPlanConfigList/:id',
          },
          {
              id: '26',
              bpid: '2',
              mpid: '2',
              name: '功能模块配置',
              icon: 'bars',
              route: '/configureList/moduleConfigList',
            },
            {
              id: '261',
              mpid: '-1',
              bpid: '25',
              name: '功能模块详情页',
              route: '/configureList/moduleConfigList/:id',
            },
          {
            id: '7',
            bpid: '1',
            name: '监控',
            icon: 'eye',
            route: '/post',
          },
          {
            id: '3',
            bpid: '1',
            name: '测试执行',
            icon: 'shopping-cart',
            route: '/execute',
          },

          {
            id: '4',
            bpid: '1',
            name: 'Request',
            icon: 'api',
            route: '/request',
          },

          {
            id: '5',
            bpid: '1',
            name: 'Recharts',
            icon: 'code-o',
          },
          {
            id: '51',
            bpid: '5',
            mpid: '5',
            name: 'LineChart',
            icon: 'line-chart',
            route: '/chart/lineChart',
          },
          {
            id: '52',
            bpid: '5',
            mpid: '5',
            name: 'BarChart',
            icon: 'bar-chart',
            route: '/chart/barChart',
          },
          {
            id: '53',
            bpid: '5',
            mpid: '5',
            name: 'AreaChart',
            icon: 'area-chart',
            route: '/chart/areaChart',
          },
          {
            id: '6',
            bpid: '1',
            name: 'Test Navigation',
            icon: 'setting',
          },
          {
            id: '61',
            bpid: '6',
            mpid: '6',
            name: 'Test Navigation1',
            route: '/navigation/navigation1',
          },
          {
            id: '62',
            bpid: '6',
            mpid: '6',
            name: 'Test Navigation2',
            route: '/navigation/navigation2',
          },
          {
            id: '621',
            bpid: '62',
            mpid: '62',
            name: 'Test Navigation21',
            route: '/navigation/navigation2/navigation1',
          },
          {
            id: '622',
            bpid: '62',
            mpid: '62',
            name: 'Test Navigation22',
            route: '/navigation/navigation2/navigation2',
          },
          {
            id: '8',
            bpid: '1',
            name: '测试结果',
            icon: 'bar-chart',
            route: '/testResult',
          },
          {
            id: '9',
            mpid: '-1',
            bpid: '1',
            name: '邮件测试结果',
            route: '/getSpecificResult/',
          },
        ]


        const { permissions } = user
        yield put({
          type: 'updateState',
          payload: {
            user,
            permissions,
            menu: list.filter(item => {
              /*原代码*/
              // if (permissions.role === EnumRoleType.ADMIN || permissions.role === EnumRoleType.DEVELOPER) {
              //   return true
              // }


              const cases = [
                permissions.visit.includes(item.id),
                item.mpid ? permissions.visit.includes(item.mpid) || item.mpid === '-1' : true,
                item.bpid ? permissions.visit.includes(item.bpid) : true,
              ]
              return cases.every(_ => _)
            }
          ),
          },
        })
        if (location.pathname === '/login') {
          yield put(routerRedux.push('/dashboard'))
        }
      } else {
        if (config.openPages && config.openPages.indexOf(location.pathname) < 0) {
          let from = location.pathname
          window.location = `${location.origin}/login?from=${from}`
        }
      }
    },

    *logout ({
      payload,
    }, { call, put }) {
      const data = yield call(logout, parse(payload))
      if (data.success) {
        yield put({ type: 'query' })
      } else {
        throw (data)
      }
    },

    *changeNavbar ({
      payload,
    }, { put, select }) {
      const { app } = yield(select(_ => _))
      const isNavbar = document.body.clientWidth < 769
      if (isNavbar !== app.isNavbar) {
        yield put({ type: 'handleNavbar', payload: isNavbar })
      }
    },

  },
  reducers: {
    updateState (state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },

    switchSider (state) {
      localStorage.setItem(`${prefix}siderFold`, !state.siderFold)
      return {
        ...state,
        siderFold: !state.siderFold,
      }
    },

    switchTheme (state) {
      localStorage.setItem(`${prefix}darkTheme`, !state.darkTheme)
      return {
        ...state,
        darkTheme: !state.darkTheme,
      }
    },

    switchMenuPopver (state) {
      return {
        ...state,
        menuPopoverVisible: !state.menuPopoverVisible,
      }
    },

    handleNavbar (state, { payload }) {
      return {
        ...state,
        isNavbar: payload,
      }
    },

    handleNavOpenKeys (state, { payload: navOpenKeys }) {
      return {
        ...state,
        ...navOpenKeys,
      }
    },
  },
}
